package org.balthie.demo.opensource.org.codehaus.plexus.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MatchPatterns
{
  private final MatchPattern[] patterns;

  private MatchPatterns(MatchPattern[] patterns)
  {
    this.patterns = patterns;
  }

  public boolean matches(String name, boolean isCaseSensitive)
  {
    String[] tokenized = MatchPattern.tokenizePathToString(name, File.separator);
    return matches(name, tokenized, isCaseSensitive);
  }

  public boolean matches(String name, String[] tokenizedName, boolean isCaseSensitive)
  {
    char[][] tokenizedNameChar = new char[tokenizedName.length][];
    for (int i = 0; i < tokenizedName.length; i++)
    {
      tokenizedNameChar[i] = tokenizedName[i].toCharArray();
    }
    return matches(name, tokenizedNameChar, isCaseSensitive);
  }

  /**
   * @author：balthie@126.com
   * @createtime ： 2022年10月31日 下午3:38:16
   * @description TODO 一句话描述
   * @since version 初始于版本 TODO 
   * @param name
   * @param tokenizedNameChar
   * @param isCaseSensitive
   * @return
   */
  public boolean matches(String name, char[][] tokenizedNameChar, boolean isCaseSensitive)
  {
    for (MatchPattern pattern : this.patterns)
    {
//      System.out.println( name + " matches " + patterns[0].regexPattern);
      if (pattern.matchPath(name, tokenizedNameChar, isCaseSensitive))
      {
        return true;
      }
    }
    return false;
  }

  public boolean matchesPatternStart(String name, boolean isCaseSensitive)
  {
    for (MatchPattern includesPattern : this.patterns)
    {
      if (includesPattern.matchPatternStart(name, isCaseSensitive))
      {
        return true;
      }
    }
    return false;
  }

  public static MatchPatterns from(String[] sources)
  {
    int length = sources.length;
    MatchPattern[] result = new MatchPattern[length];
    for (int i = 0; i < length; i++)
    {
      result[i] = MatchPattern.fromString(sources[i]);
    }
    return new MatchPatterns(result);
  }

  public static MatchPatterns from(Iterable<String> strings)
  {
    return new MatchPatterns(getMatchPatterns(strings));
  }

  private static MatchPattern[] getMatchPatterns(Iterable<String> items)
  {
    List result = new ArrayList();
    for (String string : items)
    {
      result.add(MatchPattern.fromString(string));
    }
    return (MatchPattern[])result.toArray(new MatchPattern[0]);
  }
}